package com.slkj.shilixiaoyuanapp;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.ui.user.LoginActivity;
import com.squareup.leakcanary.LeakCanary;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import cn.jpush.android.api.JPushInterface;
import zlc.season.rxdownload3.core.DownloadConfig;
import zlc.season.rxdownload3.extension.ApkInstallExtension;
import zlc.season.rxdownload3.extension.ApkOpenExtension;


/**
 * Created  on 2017/11/27
 * @author 唐开阔
 * @describe
 */

public class SlxyApplication extends Application {
    private static Context context;
    private Activity currentActivity;
    private static SlxyApplication slxyApplication;
    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        slxyApplication = this;
        if (!LeakCanary.isInAnalyzerProcess(this) && BuildConfig.DEBUG) {
            LeakCanary.install(this);
        }
        context = this;
        CrashReport.initCrashReport(getApplicationContext());
        UMConfigure.init(context,"5b5ad542a40fa304eb0002e2","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
        PlatformConfig.setWeixin("wx6d4e4c85d76c0f48","71f5a2c79b3e91df5c9e033b42130112");
        PlatformConfig.setQQZone("1107679226","qhqXqWxsTa73pvVY");
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("app", " onViewInitFinished is " + arg0);
            }
            @Override
            public void onCoreInitFinished() {
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(),  cb);
        JPushInterface.init(this);
        if (UserRequest.getInstance().isLogin()){
            JPushInterface.setAlias(this, 1,  UserRequest.getInstance().getUser().getUser().getULoginUserId()+"");
        }

        DownloadConfig.Builder builder = DownloadConfig.Builder.Companion.create(this)
                .enableDb(true)
                .setDebug(true)
                .enableNotification(false)
                .addExtension(ApkInstallExtension.class)
                .addExtension(ApkOpenExtension.class);

        DownloadConfig.INSTANCE.init(builder);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }
            public void onActivityDestroyed(Activity activity) {
                /**
                 * 防止内存泄漏
                 */
                if (currentActivity == activity){
                    currentActivity = null;
                }
            }

            /** Unused implementation **/
            public void onActivityStarted(Activity activity) {
            }

            public void onActivityResumed(Activity activity) {
                currentActivity = activity;
            }

            public void onActivityPaused(Activity activity) {
            }

            public void onActivityStopped(Activity activity) {
            }

            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }
        });
    }

    public Activity getCurrentActivity() {
        return currentActivity;
    }

    public static SlxyApplication getSlxyApplication() {
        return slxyApplication;
    }
}
